#include <Wire.h> 
#include <LiquidCrystal.h>

//took freeram from the internet
int freeRam () {
  extern int __heap_start, *__brkval; 
  int v; 
  return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); 
}

byte incomingByte;
String dataregel;
int found=0;
int exitWhile=0;
int altitude;
int maxAlt=1;
String altitudeTxt;
String maxAltTxt;
LiquidCrystal lcd(7,6,5,4,3,2);
int offSet=0;
String altPreFix="Alt. : ";
const int buttonPin = 8;
int buttonState = 0;
int actie = 0;
boolean invalidData = false;

void setup() {
  Serial.begin(38400);
  lcd.begin(16,2);
  pinMode(10, OUTPUT);
  pinMode(buttonPin, INPUT);
  lcd.setCursor(1,0);
  lcd.print("Waiting for");
  lcd.setCursor(1,1);
  lcd.print("data...");

}

void loop(){

  while (Serial.available()>0 && exitWhile==0) {
    incomingByte=Serial.read();
    //Serial.println(char(incomingByte));
    //Serial.println(incomingByte);
    
    // the code below is the check the length of the incoming height
    // it doesnt do any checking yet, but that will be implemented later.
    if (char(incomingByte)=='L'){
      Serial.print("Lengte:");
      char tmpLengte[1];
      tmpLengte[0] = Serial.read();
      int intLengte=atoi(tmpLengte);
      Serial.println(intLengte);
    }
    
    if (char(incomingByte)=='E') {
      found=0;
      exitWhile=1;
    }
    if (found==1 && ((incomingByte >= 48 && incomingByte <= 57) || incomingByte == 45 )) {
      dataregel += char(incomingByte);
    }
    if (char(incomingByte)=='B') {
        found=1;
    }
    if (((incomingByte) < 48 || (incomingByte) > 57) && incomingByte != 45 && incomingByte !=66 && incomingByte !=69 && incomingByte !=76) {
    
        Serial.print("vreemde byte:");
        Serial.print(incomingByte,DEC);
        Serial.println(incomingByte);
        invalidData = true;
    
    }
  }
  
  if (!invalidData) {
    Serial.print("Dataregel=");
    Serial.print(dataregel.length());
    Serial.println(dataregel);
    char carray[dataregel.length() + 1]; //determine size of the array
    dataregel.toCharArray(carray, sizeof(carray)); //put readStringinto an array
    int altitude = atoi(carray);
    altitude -= 20000;
    buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH) {
      actie=1;
      delay(500);  
    }
  
  
      if (actie==1) {
        if (altPreFix=="Alt. : ") {
          altPreFix="*Alt. : ";
          offSet = altitude;
          maxAlt=0;
        } else {
          altPreFix="Alt. : ";
          offSet=0;
          maxAlt=0;
        }
      }
      actie=0;
    
      Serial.print("offSet=");
      Serial.println(offSet);
    
    
    delay(50);
    
    if (dataregel.length()!=0) {
        altitude = altitude - offSet ;
        if (altitude >= maxAlt){
          maxAlt=altitude;
        }
        Serial.print("Altitude=");
        Serial.println(altitude);
        Serial.print(" maxAlt=");
        Serial.println(maxAlt);
        lcd.setCursor(1,0);
        lcd.print(altPreFix);
        lcd.print(String(altitude)+"      ");
        lcd.setCursor(1,1);
        lcd.print("                ");
        lcd.setCursor(1,1);
        lcd.print("Apogee : ");
        lcd.print(String(maxAlt));
    
      }
    
  }
  found=0;
  dataregel="";
  exitWhile=0;
  invalidData=false;
}

